<?php
class OilsController extends Controller {
	public function actionIndex($usage=null, $part = null){
		if(!empty($usage) && empty($part)){
			if($usage_id = L::r_item('gsmUsage', $usage)) {
				$parts = CHtml::listData(Motul::model()->findAll(array(
					'condition'=>'type='.$usage_id,
					'group'=>'part_id',
				)), 'part_id', 'part_id');

				foreach($parts as $i=>$part)
					$parts[$i] = L::model()->find('code=:part and type="gsmType"', array(':part'=>$part));
					
				$models = Motul::model()->findAll('type=:type', array(':type'=>$usage_id));

				$this->render('usage', array(
					'usage'=>$usage,
					'parts'=>$parts,
					'models'=>$models,
				));
				Yii::app()->end();
			} else $this->error404();
			
		}
		
		if(!empty($usage) && !empty($part)) {
			if(($usage_id = L::r_item('gsmUsage', $usage)) && ($part_id = L::r_item('gsmType', $part))) {
				
				$models = Motul::model()->findAll('type=:type and part_id=:part_id', array(':type'=>$usage_id, ':part_id'=>$part_id));
				$this->render('part', array(
					'models'=>$models,
				));
				Yii::app()->end();
			} else $this->error404();
		}
		
		$usages = L::model()->findAll('type=:type',array(':type'=>'gsmUsage'), array('order'=>'ord'));
		$this->render('index', array('usages'=>$usages));
	}
	
	public function actionView($id){

		if($model = Motul::model()->findByPK($id))
			$this->render('view', array(
				'model'=>$model
			));
		else $this->error404();
	}
	
	protected function error404(){
		throw new CHttpException(404,'Запрашиваемая страница не существует.');
		Yii::app()->end();
	}
}